package org.linuxsogood.boot.nio;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

/**
 * @author honway.liu
 * @date 2018/9/5 上午11:17
 * @email gm100861@gmail.com
 * @blog http://linuxsogood.org
 */
public class TestChannel {

    @Test
    public void method2() throws IOException {
        FileChannel inChannel = FileChannel.open(Paths.get("/opt", "video", "little", "tumblr_oz8r4qvFnu1wehz4l.mp4"), StandardOpenOption.READ);
        FileChannel outChannel = FileChannel.open(Paths.get("/Users", "m", "Downloads", "111.mp4"), StandardOpenOption.WRITE, StandardOpenOption.CREATE);

        long l = outChannel.transferFrom(inChannel, 0, inChannel.size());
        System.out.println(l);

        inChannel.close();
        outChannel.close();
    }

    @Test
    public void method() throws IOException {
        FileInputStream fis = new FileInputStream("/opt/video/little/tumblr_oz8r4qvFnu1wehz4l.mp4");
        FileOutputStream fos = new FileOutputStream("/Users/m/Downloads/111.mp4");
        FileChannel inChannel = fis.getChannel();
        FileChannel outChannel = fos.getChannel();

        ByteBuffer buffer = ByteBuffer.allocate(1024);

        while (inChannel.read(buffer) != -1) {
            buffer.flip();

            outChannel.write(buffer);

            buffer.clear();
        }

        inChannel.close();
        outChannel.close();
        fis.close();
        fos.close();
    }
}
